package com.shujia.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class MakeDataStudent {

    public static void main(String[] args) throws Exception {


        /**
         * 模拟生成数据保存到mysql中
         *
         */

        FileReader fileReader = new FileReader("spring/data/students.txt");

        BufferedReader bufferedReader = new BufferedReader(fileReader);

        String line;

        while ((line = bufferedReader.readLine()) != null) {

            String[] split = line.split(",");
            String id = split[0];
            String name = split[1];
            Integer age = Integer.parseInt(split[2]);
            String gender = split[3];
            String clazz = split[4];

            //将数据保存到数据库

            Connection con = JDBCUtil.getConnection();

            PreparedStatement stat = con.prepareStatement("insert into student(id,name,age,gender,clazz) values(?,?,?,?,?)");


            for (int i = 0; i < 1000; i++) {

                String newId = id + i;

                stat.setString(1, newId);
                stat.setString(2, name);
                stat.setInt(3, age);
                stat.setString(4, gender);
                stat.setString(5, clazz);

                //增加一行
                stat.addBatch();
            }

            //一次插入多条
            stat.executeBatch();

            System.out.println("1000条保存成功");

        }

    }
}
